<template>
  <div class="m-video" :style="`width: ${width}px; height: ${height}px;`">
    <video
        ref="veo"
        id="video"
        :style="`object-fit: ${zoom};`"
        :width="width"
        :height="height">
      您的浏览器不支持video标签。
    </video>
  </div>
</template>
<script>
export default {
  name: 'VideoPlay',
  props: {
    videoUrl: { // 视频文件url，必传，支持网络地址 https 和相对地址 require('@/assets/files/Bao.mp4')
      type: String,
      required: true,
      default: null
    },
    videoCover: { // 视频封面url，支持网络地址 https 和相对地址 require('@/assets/images/Bao.jpg')
      type: String,
      default: null
    },
    width: { // 视频播放器宽度
      type: Number,
      default: 800
    },
    height: { // 视频播放器高度
      type: Number,
      default: 450
    },
    zoom: { // video的poster默认图片和视频内容缩放规则
      type: String,
      default: 'none' // none:(默认)保存原有内容，不进行缩放; fill:不保持原有比例，内容拉伸填充整个内容容器; contain:保存原有比例，内容以包含方式缩放; cover:保存原有比例，内容以覆盖方式缩放
    }
  },
  data(){
    return{
      player:null,
    }
  },
  beforeDestroy() {
    this.player && this.player.dispose();
  },
  mounted () {
    this.play()
  },
  methods: {
    play(){
      const options = {
        sources: [{src: this.videoUrl, type: "video/hls"}],
        poster: this.videoCover,
        autoplay: true,
        controls: true
      }
      this.player = TCPlayer('video', options);
      this.player.on('play', () => {

      })
      this.player.on('error', (e) => {
        console.log(e)
        this.$message.warning('发生错误');
      })
    }
  }
}
</script>
<style lang="less" scoped>
.m-video {
  display: inline-block;
  position: relative;
  background: #000;
  cursor: pointer;
  &:hover {
    .u-play {
      opacity: 0.9;
    }
  }
  .u-play {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    fill: none;
    color: #FFF;
    pointer-events: none;
    opacity: 0.7;
    transition: opacity .3s;
  }
  .hidden {
    display: none;
  }
}
</style>
